package com.ruoyi.marketing.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.marketing.domain.ActivityUser;
import com.ruoyi.marketing.domain.ActivityUserTask;
import com.ruoyi.marketing.domain.Task;
import com.ruoyi.marketing.mapper.ActivityUserTaskMapper;
import com.ruoyi.marketing.service.IActivityUserService;
import com.ruoyi.marketing.service.IActivityUserTaskService;
import com.ruoyi.marketing.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 营销活动Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-28
 */
@Service
public class ActivityUserTaskServiceImpl extends ServiceImpl<ActivityUserTaskMapper, ActivityUserTask> implements IActivityUserTaskService {
    @Autowired
    private ITaskService marketTaskService;
    @Autowired
    private IActivityUserTaskService activityUserTaskService;
    @Autowired
    private IActivityUserService activityUserService;

    @Override
    public void completeTask(Long activityId, Long taskId, SysUser user) {
        //查询任务和用户的关系表
        //构建查询条件
        LambdaQueryWrapper<ActivityUserTask> lqw = new LambdaQueryWrapper<ActivityUserTask>()
                .eq(ActivityUserTask::getActivityId, activityId)
                .eq(ActivityUserTask::getUserId, user.getUserId())
                .eq(ActivityUserTask::getTaskId, taskId);
        ActivityUserTask userTask = activityUserTaskService.getOne(lqw);
        //查询对应任务
        Task task = marketTaskService.getById(taskId);
        //获取用户注册时间间隔
        //获取注册时间
        Date createTime = user.getCreateTime();
        //获取时间间隔
        Date date = new Date();
        int day = (int) (date.getTime() - createTime.getTime()) / (1000 * 3600 * 24);
        //判断当前任务是否待完成
        if (userTask.getStatus() == 1) {
            //所有用户参与
            if (task.getTime() == 2) {
                //判断参与活动的次数限制

               /* //限制一次
                if (task.getFlimit() == 0) {
                    //直接完成
                }
                //每日刷新
                else {
                    //直接完成
                }*/
                //完成并且用户抽奖次数+1
                setprize(activityId, user, userTask);
                return;
            }
            //老用户参与
            else if (task.getTake() == 1 && day > 30) {
                setprize(activityId, user, userTask);
                return;
            }
            //新用户参与
            else {
                setprize(activityId, user, userTask);
                return;
            }
        }
        //判断该任务是否已经被完成
        throw new ServiceException("没有参与资格");
    }

    private void setprize(Long activityId, SysUser user, ActivityUserTask userTask) {
        userTask.setStatus(0);
        userTask.setTime(new Date());
        super.updateById(userTask);
        //判断用户是否第一次参加该活动
        //构建查询条件
        LambdaQueryWrapper<ActivityUser> lw = new LambdaQueryWrapper<ActivityUser>()
                .eq(ActivityUser::getActivityId, activityId)
                .eq(ActivityUser::getUserId, user.getUserId());
        ActivityUser activityUser = activityUserService.getOne(lw);
        //第一次参加活动
        if (activityId == null) {
            //添加数据
            ActivityUser activityUser1 = new ActivityUser();
            activityUser1.setActivityId(activityId);
            activityUser1.setUserId(user.getUserId());
            activityUser1.setSurplus(1);
            activityUser1.setPrize(0);
            activityUserService.save(activityUser1);
        }
        //不是第一次
        else {
            //抽奖次数+1
            activityUser.setSurplus(activityUser.getSurplus() + 1);
            activityUserService.updateById(activityUser);
        }
    }
}
